package com.uzai.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther: xiaoyuexl
 * @Date: 2023/2/6 11:49
 * @Description:
 */
public final class RegularUtil {
    /**
     * 获取查券内容类型（0=未识别到；1=链接；2=商品id；3=淘口令）
     * @param content
     * @return
     */
    public static int getTbQueryType(String content) {
        //先判断是不是链接
        Pattern pattern = Pattern.compile("^(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]$");
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            return 1;
        }

        //判断是不是商品id
        pattern = Pattern.compile("^([0-9]{5,15}|[a-zA-Z0-9]{10,30}-[a-zA-Z0-9]{15,30})$");
        matcher = pattern.matcher(content);
        if (matcher.find()) {
            return 2;
        }

        return 3;
    }

    /**
     * 官方采集群消息 淘口令提取 (?<=\[淘口令\|)([0-9a-zA-Z\s\-\(\)\\\/\:\$￥]{10,})(?=\])
     * @param content
     * @return
     */
    public static String getOfficialGroupMsgByTkl(String content) {
        Pattern pattern = Pattern.compile("(?<=\\[淘口令\\|)([0-9a-zA-Z\\s\\-\\(\\)\\\\\\/\\:\\$￥]{15,})(?=\\])");
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }

    /**
     * 官方采集群消息 拼多多链接提取
     * @param content
     * @return
     */
    public static List<String> getOfficialGroupMsgByPddUrl(String content) {
        List<String> urlList = new ArrayList<>();
        Pattern pattern = Pattern.compile("(?<=\\[拼多多\\|)(.+?)(?=\\])");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            urlList.add(matcher.group());
        }
        return urlList;
    }

    /**
     * 官方采集群消息 京东链接提取
     * @param content
     * @return
     */
    public static List<String> getOfficialGroupMsgByJdUrl(String content) {
        List<String> urlList = new ArrayList<>();
        Pattern pattern = Pattern.compile("(?<=\\[京东\\|)(.+?)(?=\\])");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            urlList.add(matcher.group());
        }
        return urlList;
    }

    /**
     * 官方采集群消息 唯品会链接提取
     * @param content
     * @return
     */
    public static List<String> getOfficialGroupMsgByVphUrl(String content) {
        List<String> urlList = new ArrayList<>();
        Pattern pattern = Pattern.compile("(?<=\\[唯品会\\|)(.+?)(?=\\])");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            urlList.add(matcher.group());
        }
        return urlList;
    }

}
